import json
import requests
import os
import django

os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE',
    'drf_study.settings'
)
django.setup()
from user.models import Article, User

cookies = {
    'uuid_tt_dd': '10_19730457420-1736826227371-941133',
    'fid': '20_38598685936-1736826227297-024157',
    'historyList-new': '%5B%5D',
    'UserName': '2401_84436190',
    'UserInfo': '2d8952fec8a149148bd783b51f719c31',
    'UserToken': '2d8952fec8a149148bd783b51f719c31',
    'UserNick': '2401_84436190',
    'AU': '408',
    'UN': '2401_84436190',
    'BT': '1736945019001',
    'p_uid': 'U010000',
    'csdn_newcert_2401_84436190': '1',
    'c_ins_prid': '-',
    'c_ins_rid': '1738575720445_432525',
    'c_ins_fref': 'https://www.csdn.net/',
    'c_ins_fpage': '/index.html',
    'c_ins_um': '-',
    'ins_first_time': '1738575708890',
    'FCNEC': '%5B%5B%22AKsRol9fAilc8FgWjONhouFEw4ZIAmECo9RwirlWIbWS3pdtM9vfcQ3OsDQveezmQ4vaUMtjuKNFoFAVDfNXl3hari0Fj2UJdLULhcze_sU_qHvEUKr6a6k3G4KwDiyCpvjD7-XZomAYCFB1PzaSs5IFVNXbufgEWw%3D%3D%22%5D%5D',
    'dp_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ODExMzE5NiwiZXhwIjoxNzQwMzY1NDY2LCJpYXQiOjE3Mzk3NjA2NjYsInVzZXJuYW1lIjoiMjQwMV84NDQzNjE5MCJ9.zAe4bnoeJcZf4uMlBSl45YsBXjQHMb4Ov4tEQ2RU4PQ',
    'c_dl_fref': 'https://so.csdn.net/so/search',
    'c_ab_test': '1',
    '_clck': '19qa9in%7C2%7Cftm%7C0%7C1840',
    'c_dl_prid': '1739760790119_128832',
    'c_dl_rid': '1740104528379_852503',
    'c_dl_fpage': '/download/h363659487/89425417',
    'c_dl_um': '-',
    '__gads': 'ID=b02f68c827bcc4d8:T=1736826270:RT=1740104532:S=ALNI_Mb5-BytqcsHoyA3jfxnw0FkkTpiKQ',
    '__gpi': 'UID=00000fcdf33ba86f:T=1736826270:RT=1740104532:S=ALNI_MZ5MHzZ2sNi7jZLUEvmXtwC62tqqg',
    '__eoi': 'ID=4e7572574c8f65fe:T=1736826270:RT=1740104532:S=AA-AfjZYfWlnc_BgQdy4iEIu1vEV',
    '_clsk': '15z7y0z%7C1740104591370%7C2%7C0%7Cx.clarity.ms%2Fcollect',
    'fe_request_id': '1740149607474_4975_0290201',
    'dc_session_id': '10_1740187088288.808933',
    'c_first_ref': 'www.baidu.com',
    'c_first_page': 'https%3A//www.csdn.net/',
    'c_segment': '13',
    'c-sidebar-collapse': '0',
    'Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac': '1740060594,1740100853,1740149599,1740187081',
    'HMACCOUNT': 'BB6FD7A3A1F38641',
    'dc_sid': '676f6777a1844cbdb51e62ac518365d5',
    'c_page_id': 'default',
    'c_ref': 'https%3A//www.baidu.com/link',
    'c_pref': 'https%3A//www.baidu.com/link',
    'c_dsid': '11_1740187391768.755220',
    'log_Id_pv': '5',
    'Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac': '1740187392',
    'creativeSetApiNew': '%7B%22toolbarImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20231011044944.png%22%2C%22publishSuccessImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20240229024608.png%22%2C%22articleNum%22%3A0%2C%22type%22%3A0%2C%22oldUser%22%3Afalse%2C%22useSeven%22%3Atrue%2C%22oldFullVersion%22%3Afalse%2C%22userName%22%3A%222401_84436190%22%7D',
    'log_Id_view': '250',
    'dc_tos': 'ss296s',
    'log_Id_click': '3',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'origin': 'https://www.csdn.net',
    'priority': 'u=1, i',
    'referer': 'https://www.csdn.net/',
    'sec-ch-ua': '"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
    # 'cookie': 'uuid_tt_dd=10_19730457420-1736826227371-941133; fid=20_38598685936-1736826227297-024157; historyList-new=%5B%5D; UserName=2401_84436190; UserInfo=2d8952fec8a149148bd783b51f719c31; UserToken=2d8952fec8a149148bd783b51f719c31; UserNick=2401_84436190; AU=408; UN=2401_84436190; BT=1736945019001; p_uid=U010000; csdn_newcert_2401_84436190=1; c_ins_prid=-; c_ins_rid=1738575720445_432525; c_ins_fref=https://www.csdn.net/; c_ins_fpage=/index.html; c_ins_um=-; ins_first_time=1738575708890; FCNEC=%5B%5B%22AKsRol9fAilc8FgWjONhouFEw4ZIAmECo9RwirlWIbWS3pdtM9vfcQ3OsDQveezmQ4vaUMtjuKNFoFAVDfNXl3hari0Fj2UJdLULhcze_sU_qHvEUKr6a6k3G4KwDiyCpvjD7-XZomAYCFB1PzaSs5IFVNXbufgEWw%3D%3D%22%5D%5D; dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ODExMzE5NiwiZXhwIjoxNzQwMzY1NDY2LCJpYXQiOjE3Mzk3NjA2NjYsInVzZXJuYW1lIjoiMjQwMV84NDQzNjE5MCJ9.zAe4bnoeJcZf4uMlBSl45YsBXjQHMb4Ov4tEQ2RU4PQ; c_dl_fref=https://so.csdn.net/so/search; c_ab_test=1; _clck=19qa9in%7C2%7Cftm%7C0%7C1840; c_dl_prid=1739760790119_128832; c_dl_rid=1740104528379_852503; c_dl_fpage=/download/h363659487/89425417; c_dl_um=-; __gads=ID=b02f68c827bcc4d8:T=1736826270:RT=1740104532:S=ALNI_Mb5-BytqcsHoyA3jfxnw0FkkTpiKQ; __gpi=UID=00000fcdf33ba86f:T=1736826270:RT=1740104532:S=ALNI_MZ5MHzZ2sNi7jZLUEvmXtwC62tqqg; __eoi=ID=4e7572574c8f65fe:T=1736826270:RT=1740104532:S=AA-AfjZYfWlnc_BgQdy4iEIu1vEV; _clsk=15z7y0z%7C1740104591370%7C2%7C0%7Cx.clarity.ms%2Fcollect; fe_request_id=1740149607474_4975_0290201; dc_session_id=10_1740187088288.808933; c_first_ref=www.baidu.com; c_first_page=https%3A//www.csdn.net/; c_segment=13; c-sidebar-collapse=0; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1740060594,1740100853,1740149599,1740187081; HMACCOUNT=BB6FD7A3A1F38641; dc_sid=676f6777a1844cbdb51e62ac518365d5; c_page_id=default; c_ref=https%3A//www.baidu.com/link; c_pref=https%3A//www.baidu.com/link; c_dsid=11_1740187391768.755220; log_Id_pv=5; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1740187392; creativeSetApiNew=%7B%22toolbarImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20231011044944.png%22%2C%22publishSuccessImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20240229024608.png%22%2C%22articleNum%22%3A0%2C%22type%22%3A0%2C%22oldUser%22%3Afalse%2C%22useSeven%22%3Atrue%2C%22oldFullVersion%22%3Afalse%2C%22userName%22%3A%222401_84436190%22%7D; log_Id_view=250; dc_tos=ss296s; log_Id_click=3',
}

params = {
    'page': '1',
    'componentIds': 'silkroad-pre-home-list',
    'cate1': 'java',
}

response = requests.get('https://cms-api.csdn.net/v1/web_home/select_content', params=params, cookies=cookies,
                        headers=headers)
response = json.loads(response.text)
blog_list = response['data']['silkroad-pre-home-list']['info']
for blog in blog_list:
    try:
        nickname = blog["extend"]["nickname"]
        title = blog["extend"]['title']
        desc = blog["extend"]['desc']
        pic = blog["extend"]['pic']
        url = blog["extend"]['url']
        user = User.objects.get_or_create(name=nickname)
        Article.objects.get_or_create(
            user=user,
            title=title,
            desc=desc,
            pic_url=pic,
            url=url
        )
    except Exception as e:
        print(f"异常错误为{e}")
